package team.lourd.network.system.own.repository.jpa;

import com.querydsl.core.types.dsl.StringPath;
import org.springframework.data.querydsl.QuerydslPredicateExecutor;
import org.springframework.data.querydsl.binding.QuerydslBinderCustomizer;
import org.springframework.data.querydsl.binding.QuerydslBindings;
import org.springframework.data.repository.PagingAndSortingRepository;
import team.lourd.network.system.own.domain.QRecord;
import team.lourd.network.system.own.domain.Record;
import team.lourd.network.system.querydsl.ExtendedQuerydslJpaRepository;

import java.io.UnsupportedEncodingException;

public interface RecordRepository  extends PagingAndSortingRepository<Record, Long>, QuerydslBinderCustomizer<QRecord>, QuerydslPredicateExecutor<Record>, ExtendedQuerydslJpaRepository<Record, Long> {

    default void customize(QuerydslBindings bindings, QRecord record) {
        bindings.bind(String.class).first((StringPath path, String value) -> {
            // 中文乱码解决问题
            try {
                String newValue=new String(value.getBytes("iso-8859-1"),"utf-8");
                return  path.contains(newValue);
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
                return  null;
            }
        });
    }
}